package com.rabbitmq.web;

import com.rabbitmq.utils.RabbitDynamicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.concurrent.CountDownLatch;

/**
 * @Author: TongRui乀
 * @Date: 2019/8/2 13:19
 * @description：
 */
@Slf4j
@RestController
@RequestMapping("rabbit")
public class RabbitController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private ThreadPoolTaskExecutor threadPoolTaskExecutor;

    @Autowired
    private Environment env;

    @Autowired
    private RabbitDynamicUtil rabbitDynamicUtil;

    @GetMapping("multiConsumer/{threadNumber}")
    public String multiConsumer(@PathVariable Integer threadNumber){

        CountDownLatch countDownLatch = new CountDownLatch(threadNumber);

        for (int i = 0; i < threadNumber; i++) {

            final int num = i;

            threadPoolTaskExecutor.execute(()->{

               try {
                   rabbitTemplate.setExchange(env.getProperty("order.exchange.name"));
                   rabbitTemplate.setRoutingKey(env.getProperty("order.routing.key.name"));

                   String message = "kill order -" + num;

                   rabbitTemplate.convertAndSend(MessageBuilder.withBody(message.getBytes()).build());
               }catch (Exception e){

               }finally {
                   countDownLatch.countDown();
               }
            });
        }

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        threadPoolTaskExecutor.shutdown();

        return "SUCCESS";
    }


    @GetMapping("multiQueue/{message}")
    public String multiQueue(@PathVariable String message){

        rabbitTemplate.setExchange("exchange.local.log");
        rabbitTemplate.setRoutingKey("routing.key.local.log.multi");

        this.rabbitTemplate.convertAndSend(MessageBuilder.withBody(message.getBytes()).build());

        return "SUCCESS";
    }

    @GetMapping("test")
    public String test(){

        HashMap<String, String> map = new HashMap<>();

        this.rabbitTemplate.convertAndSend(env.getProperty("log.exchange.name"), env.getProperty("log.multi.consumer.routing.key.name"), "1111");

        return "SUCCESS";
    }


    @GetMapping("delay")
    public String delay(String message){

        log.info("正在创建延迟队列消息:{}", System.currentTimeMillis());

        this.rabbitTemplate.convertAndSend(env.getProperty("order.delay.exchange.name"), env.getProperty("order.delay.queue.key"), message, message1 -> {
            message1.getMessageProperties().setDelay(1000 * 60 );
            return message1;
        });

        return "SUCCESS";
    }


    @GetMapping("create")
    public String createRabbit(String queueName, String exchangeName, String bindingKey){


        rabbitDynamicUtil.createBinding(rabbitDynamicUtil.createQueue(queueName), rabbitDynamicUtil.createDelayedExchange(exchangeName), bindingKey);

        return "SUCCESS";
    }

    @GetMapping("deleteQueue")
    public String deleteQueue(String queueName){

        rabbitDynamicUtil.deleteQueue(queueName);

        return "SUCCESS";
    }

    @GetMapping("deleteExchange")
    public String deleteExchange(String exchangeName){

        rabbitDynamicUtil.deleteExchange(exchangeName);

        return "SUCCESS";
    }

    @GetMapping("send")
    public String send(String exchangeName, String routingKey, String message, Integer delayed){

        rabbitDynamicUtil.sendDelayedMessageToExchange(exchangeName, routingKey, message, delayed);

        return "SUCCESS";
    }

    @GetMapping("receive")
    public String send(String queueName){

        return rabbitDynamicUtil.receiveFromQueue(queueName);
    }

    @GetMapping("addListener")
    public String addListener(String queueName){

        rabbitDynamicUtil.addListener(queueName);

        return "SUCCESS";
    }


}
